import java.util.*;

class Student {
    private String studentId;
    private String name;
    private Map<String, Integer> scores;

    public Student(String studentId, String name) {
        this.studentId = studentId;
        this.name = name;
        this.scores = new HashMap<>();
    }

    public String getStudentId() {
        return studentId;
    }

    public String getName() {
        return name;
    }

    public Map<String, Integer> getScores() {
        return scores;
    }

    public void addScore(String course, int score) {
        scores.put(course, score);
    }

    public double getAverageScore() {
        if (scores.isEmpty()) {
            return 0;
        }
        int sum = 0;
        for (int score : scores.values()) {
            sum += score;
        }
        return (double) sum / scores.size();
    }
}

class GradeManager {
    private Map<String, Student> students;

    public GradeManager() {
        this.students = new HashMap<>();
    }

    public void addStudent(String studentId, String name) {
        if (!students.containsKey(studentId)) {
            students.put(studentId, new Student(studentId, name));
        } else {
            System.out.println("学生ID已存在！");
        }
    }

    public void addScore(String studentId, String course, int score) {
        Student student = students.get(studentId);
        if (student != null) {
            student.addScore(course, score);
        } else {
            System.out.println("未找到该学生！");
        }
    }

    public void queryStudent(String studentId) {
        Student student = students.get(studentId);
        if (student != null) {
            System.out.println("学生ID: " + student.getStudentId());
            System.out.println("姓名: " + student.getName());
            System.out.println("成绩: " + student.getScores());
            System.out.println("平均分: " + student.getAverageScore());
        } else {
            System.out.println("未找到该学生！");
        }
    }

    public void statisticsByCourse(String course) {
        int count = 0;
        int sum = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;

        for (Student student : students.values()) {
            if (student.getScores().containsKey(course)) {
                int score = student.getScores().get(course);
                count++;
                sum += score;
                if (score > max) {
                    max = score;
                }
                if (score < min) {
                    min = score;
                }
            }
        }

        if (count > 0) {
            System.out.println(course + "课程统计：");
            System.out.println("参与人数: " + count);
            System.out.println("平均分: " + (double) sum / count);
            System.out.println("最高分: " + max);
            System.out.println("最低分: " + min);
        } else {
            System.out.println("没有该课程的成绩记录！");
        }
    }

    public void showAllStudents() {
        if (students.isEmpty()) {
            System.out.println("暂无学生记录！");
            return;
        }
        for (Student student : students.values()) {
            queryStudent(student.getStudentId());
            System.out.println("-------------------");
        }
    }
}

public class StudentGradeManagementSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        GradeManager manager = new GradeManager();

        while (true) {
            System.out.println("\n学生成绩管理系统");
            System.out.println("1. 添加学生");
            System.out.println("2. 添加成绩");
            System.out.println("3. 查询学生");
            System.out.println("4. 课程统计");
            System.out.println("5. 显示所有学生");
            System.out.println("0. 退出系统");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    System.out.print("请输入学生ID: ");
                    String id = scanner.nextLine();
                    System.out.print("请输入学生姓名: ");
                    String name = scanner.nextLine();
                    manager.addStudent(id, name);
                    break;
                case 2:
                    System.out.print("请输入学生ID: ");
                    id = scanner.nextLine();
                    System.out.print("请输入课程名称: ");
                    String course = scanner.nextLine();
                    System.out.print("请输入成绩: ");
                    int score = scanner.nextInt();
                    scanner.nextLine();
                    manager.addScore(id, course, score);
                    break;
                case 3:
                    System.out.print("请输入学生ID: ");
                    id = scanner.nextLine();
                    manager.queryStudent(id);
                    break;
                case 4:
                    System.out.print("请输入课程名称: ");
                    course = scanner.nextLine();
                    manager.statisticsByCourse(course);
                    break;
                case 5:
                    manager.showAllStudents();
                    break;
                case 0:
                    System.out.println("感谢使用系统，再见！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }
}    